﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Text.RegularExpressions;

namespace Member.Client
{
    public partial class SendComment : Form
    {
        public bool LoginFlag = false;
        List<Model.SendUsers> usersList = new List<Model.SendUsers>();
        public SendComment()
        {
            InitializeComponent();
        }

        private void SendComment_Load(object sender, EventArgs e)
        {
            Model.SendUsers user1 = new Model.SendUsers();
            user1.ID = 1; user1.Name = "lat37ie309qdu2@sohu.com"; user1.Passwd = "43qhs9v2b";
            usersList.Add(user1);
            Model.SendUsers user2 = new Model.SendUsers();
        }

        private void btnAddUsers_Click(object sender, EventArgs e)
        {
            Model.SendUsers user = new Model.SendUsers();
            for (int i = 0; i < usersList.Count; i++)
            {
                user = usersList[i];
                ListViewItem list = new ListViewItem(user.ID.ToString());
                list.SubItems.Add(user.Name);
                list.SubItems.Add("*******");
                listUser.Items.Add(list);
            }
            tbRunLogs.Text = ("共添加【" + usersList.Count + "】用户\r\n");
        }

        private void btnSendStart_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(Send);
            thread.IsBackground = true;
            Control.CheckForIllegalCrossThreadCalls = false;
            thread.Start();

        }


        void Send()
        {
            tbRunLogs.AppendText("启动评论......\r\n");

            Model.SendUsers users = new Model.SendUsers();
            Model.NetGet get = new Model.NetGet();
            Model.NetGet Send = new Model.NetGet();
            Model.NetGet getComment = new Model.NetGet();

            for (int i = 0; i < usersList.Count; i++)
            {
                users = usersList[i];
                tbRunLogs.AppendText("用户名：【" + users.Name + "】开始登陆...\r\n");

                string html = Net.NetCommon.GetCode("https://login.360.cn/?o=sso&m=getToken&func=Login.tokenCallback&userName=" + users.Name);
                Match match = Regex.Match(html, @"""token"":""(?<token>.+?)""}");
                string token = match.Groups["token"].Value;
                get = Net.NetCommon.SendComment("https://login.360.cn/?o=sso&m=login&from=i360&func=loginCallback&userName=" + users.Name + "&password=" + Net.Brush.md5(users.Passwd) + "&isKeepAlive=1&token=" + token + "&captFlag=1&captId=i360", "");
                match = Regex.Match(get.Html, @"""errno"":(?<errno>.+?),.*""s"":""(?<s>.+?)""}");
                string errno = "111";
                string s = null;
                try
                {
                    errno = match.Groups["errno"].Value;
                    s = match.Groups["s"].Value;
                }
                catch { errno = "220"; s = null; }
                if (errno.Equals("0") && s != null)
                {
                    match = Regex.Match(get.Cookie, "Q=(?<Q>.+?);.*T=(?<T>.+?);");
                    string cookie = "Q=" + match.Groups["Q"].Value + ";T=" + match.Groups["T"].Value;
                    tbRunLogs.AppendText("登陆成功，准备发布评论.........\r\n");
                    Send = Net.NetCommon.SendComment("http://intf.baike.360.cn/index.php?c=message&a=addmessage&name=" + Uri.EscapeUriString("爱情连连看 Android_com.jiayou") + "&type=best&content=" + Uri.EscapeUriString("~~~~~~~~~~") + "&callback=clogin.onPost", cookie);
                    Net.NetCommon.SendComment("http://intf.baike.360.cn/index.php?c=message&a=getmessage&name=" + Uri.EscapeUriString("爱情连连看 Android_com.jiayou") + "&start=0&count=5&callback=cm.onSuccessComment&type=all", cookie);
                    match = Regex.Match(Send.Html, @"errno"":(?<errno>.+?),");
                    string retSuccess = match.Groups["errno"].Value;
                    if (retSuccess.Equals("0")) 
                    {
                        tbRunLogs.AppendText("评论发布成功，准备进行下一条\r\n");
                    }
                    else
                    {
                        if (retSuccess.Equals("-1")) { tbRunLogs.AppendText("发布失败。参数错误或登录失败。\r\n"); }
                        if (retSuccess.Equals("-2")) { tbRunLogs.AppendText("发布失败。操作过于平凡，请稍后尝试\r\n"); }
                        if (retSuccess.Equals("-3")) { tbRunLogs.AppendText("发布失败。请不要连续提交相同内容。\r\n"); }
                    }
                }
                else
                {
                    tbRunLogs.AppendText("发布失败。参数错误或登录失败。\r\n");
                }
                if (i >= usersList.Count - 1)
                { tbRunLogs.AppendText("发布完了。\r\n\r\n"); }
                else { tbRunLogs.AppendText("进行下一个发布开始\r\n\r\n"); }


            }
        }

    }
}
